﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Data;

namespace Camelot.SharePointIntegration
{
    public static class Utilities
    {
        public static string ExecuteProcess(string command, string arguments, int timeout, byte[] file)
        {
            var p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.FileName = command;
            p.StartInfo.Arguments = arguments;
            p.Start();

            using (var writer = new BinaryWriter(p.StandardInput.BaseStream))
            {
                writer.Write(file);
                writer.Flush();
            }

            var output = p.StandardOutput.ReadToEnd();
            var error = p.StandardError.ReadToEnd();
            p.WaitForExit(timeout);

            return string.IsNullOrEmpty(error) ? output : error;
        }

        public static string FtpUploadFile(string uri, byte[] data, string username, string password)
        {
            var request = (FtpWebRequest)FtpWebRequest.Create(uri);
            request.Credentials = new NetworkCredential(username, password);
            request.KeepAlive = true;
            request.UseBinary = true;
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.ContentLength = data.Length;

            string statusDescription;

            using (var requestStream = request.GetRequestStream())
            {
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
            }

            using (var response = (FtpWebResponse)request.GetResponse())
            {
                statusDescription = response.StatusDescription;
                response.Close();
            }

            return statusDescription;
        }

        public static string MakeSafeWindowsFileName(string filename)
        {
            var stringBuilder = new StringBuilder(filename);
            foreach (char c in System.IO.Path.GetInvalidFileNameChars())
            {
                stringBuilder.Replace(c, '_');
            }
            return stringBuilder.ToString();
        }

        public static void ExecuteDataTable(string sql, Camelot.SharePointConnector.Data.SharePointConnection connection, DataTable result)
        {
            var adapter = new Camelot.SharePointConnector.Data.SharePointDataAdapter();
            adapter.SelectCommand = new Camelot.SharePointConnector.Data.SharePointCommand(sql, connection);
            adapter.Fill(result);
            adapter.SelectCommand.Connection = null;
            adapter.SelectCommand.Dispose();
            adapter.SelectCommand = null;
            adapter.Dispose();
            adapter = null;
        }
    }
}
